// proxySubShopAdd.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

const app = getApp()
var util = require("../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {
      shop: "",
      phone: "",
      address: "",
      rate: "",
      refund: 5,
      id:"",
      name: "",
    },
    refundOption: [
      { text: '5分钟内', value: 5 },
      { text: '10分钟内', value: 10 },
      { text: '30分钟内', value: 30 },
      { text: '关闭', value: 0 }
    ],

    surplusRate: '',

    submitUrl:'agent/submitShop',
  },
  // 表单数值改变
  onChange: function (detail, event) {// detail 为当前输入的值
    var that = this;
    that.setData({
      [detail]: event.detail
    })
  },
  shopChange: function (event) {
    this.onChange('detail.shop', event)
  },
  telChange: function (event) {
    this.onChange('detail.phone', event)
  },
  addChange: function (event) {
    this.onChange('detail.address', event)
  },
  rateChange: function (event) {
    this.onChange('detail.rate', event)
  },
  idChange: function (event) {
    this.onChange('detail.id', event)
  },
  nameChange: function (event) {
    this.onChange('detail.name', event)
  },
  rateLimit: function (event) {
    var surplusRate = this.data.surplusRate
    if (event.detail.value > surplusRate) {
      this.setData({
        ['detail.rate']: surplusRate
      })
    }
    if (event.detail.value < 0) {
      this.setData({
        ['detail.rate']: 0
      })
    }
  },
  refundChange:function(event){
    var that = this;
    that.setData({
      ['detail.refund']: event.detail
    })
  },
  // 保存
  save: function () {
    var that = this;
    var surplusRate = this.data.surplusRate
    if (that.data.rate > surplusRate) {
      that.setData({
        ['detail.rate']: surplusRate
      })
    }
    var detail = that.data.detail;
    var data = {
      shopName: detail.shop,
      tel: detail.phone,
      address: detail.address,
      commission: detail.rate,
      id: detail.id,
      name: detail.name,
      refund:detail.refund,
      latitude: detail.latitude,
      longitude: detail.longitude
    };
    var submitUrl = that.data.submitUrl
    wx.showLoading({
      title: '加载中...',
    })
    if (detail.shop !== '' && detail.phone !== '' && detail.address !== '' && detail.rate !== '') {
      // 验证这个用户名称
      if(detail.id!==''){
        app.post('agent/verifyShopID', data).then((res) => {
          wx.hideLoading();
           if(res.code == 404) {
            wx.showToast({
              title: res.message,
              icon: 'loading',
              duration: 2000
            })
          }
        }).catch((errMsg) => {
          console.log(errMsg);
          wx.hideLoading();
          wx.showToast({
            title: errMsg,
            icon: 'loading',
            duration: 2000
          })
        })
      }

      app.post(submitUrl, data).then((res) => {
        wx.hideLoading();
        if (res.code == 200) {
          wx.showToast({
            title: "添加成功",
            icon: 'success',
            duration: 2000
          })
          var pages = getCurrentPages();
          var beforePage = pages[pages.length - 2];
          beforePage.onPullDownRefresh();
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.showToast({
            title: res.message,
            icon: 'loading',
            duration: 2000
          })
        }
      }).catch((errMsg) => {
        console.log(errMsg);
        wx.hideLoading();
        wx.showToast({
          title: errMsg,
          icon: 'loading',
          duration: 2000
        })
      })

    } else {
      wx.showToast({
        title: '请完善您的资料',
        icon: 'none',
        duration: 4000
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      surplusRate: app.globalData.agentRate,
      submitUrl:options.submitUrl
    })
  },
  // 获取详细地址
  getAddress: function () {
    var that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        wx.chooseLocation({
          latitude,
          longitude,
          success: function (res) {
            that.setData({
              ['detail.address']: res.address,
              ['detail.latitude']: res.latitude,
              ['detail.longitude']: res.longitude
            })
          },
        })
      }, fail(res) {
        Toast.fail("请检查您的微信或小程序是否开启定位服务")
      }
    })
  },
})